package cn.mall.member.service.impl;


import cn.mall.common.constant.MemberResponseVo;
import cn.mall.common.utils.R;
import cn.mall.member.VO.UserLoginVo;

import cn.mall.member.service.MemberService;
import org.springframework.stereotype.Service;

import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.mall.common.utils.PageUtils;
import cn.mall.common.utils.Query;

import cn.mall.member.dao.MemberDao;
import cn.mall.common.vo.MemberEntity;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public MemberResponseVo login(UserLoginVo loginVo) {
        String account = loginVo.getAccount();
        String password = loginVo.getPassword();

        //数据库查询密文密码
        MemberEntity entity = baseMapper.selectOne(new QueryWrapper<MemberEntity>()
                .eq("username",account).or().eq("mobile", account));
        if (entity != null) {
            //密文
            String passwordDb = entity.getPassword();
            //密码匹配
            boolean matches=passwordDb.equals(password);
            if (matches) {
                return entity;
            } else {
                return null;
            }
        }
        return null;
    }

    @Override
    public R selectAndRegist(String name, String tel) {
        MemberEntity entity = baseMapper.selectOne(new QueryWrapper<MemberEntity>()
                .eq("username",name).or().eq("mobile", tel));
        if (entity!=null){
            return R.ok().put("data",entity);
        }else{
            return  R.error("用户名和密码被注册");
        }
    }
}